package com.riku.security.controller;

import com.riku.security.common.SeCommonResponse;
import com.riku.security.common.SeSecurityException;
import com.riku.security.common.SnowFlake;
import com.riku.security.model.SeRole;
import com.riku.security.model.SeRoleMenu;
import com.riku.security.model.SeRoleUser;
import com.riku.security.model.SeUserParam;
import com.riku.security.service.SeRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 1 on 2021/5/18.
 */
@Api(value = "security-role",description = "权限-角色")
@RestController
@RequestMapping("/se/role/")
public class SeRoleController {

    @Autowired
    private SeRoleService seRoleService;

    @ApiOperation("新增角色")
    @PostMapping("insertRole")
    public SeCommonResponse insertRole(
            @ApiParam(required = true) @RequestParam(required = true) String roleName,
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = false) @RequestParam(required = false) String descr,
            @ApiParam(required = true) @RequestParam(required = true) Integer status,
            @ApiParam(required = false) @RequestParam(required = false) String menuIds
    ) {
        seRoleService.insertSeRole(serviceCode,roleName,descr,status,menuIds);
        return SeCommonResponse.success();
    }

    @ApiOperation("修改角色")
    @PutMapping("updateRole")
    public SeCommonResponse updateRole(
            @ApiParam(required = true) @RequestParam(required = true) Long id,
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = true) @RequestParam(required = true) String roleName,
            @ApiParam(required = false) @RequestParam(required = false) String descr,
            @ApiParam(required = true) @RequestParam(required = true) Integer status,
            @ApiParam(required = false) @RequestParam(required = false) String menuIds
    ) {
        seRoleService.updateRole(serviceCode,id,roleName,descr,status,menuIds);
        return SeCommonResponse.success();
    }

    @ApiOperation("启用/停用角色")
    @PutMapping("statusRole")
    public SeCommonResponse statusRole(
            @ApiParam(required = true) @RequestParam(required = true) Long id,
            @ApiParam(required = true) @RequestParam(required = true) Integer status
    ) {
        seRoleService.statusRole(id,status);
        return SeCommonResponse.success();
    }

    @ApiOperation("删除角色")
    @DeleteMapping("deleteRole")
    public SeCommonResponse deleteRole(
            @ApiParam(required = true) @RequestParam(required = true) Long id
    ) {
        seRoleService.deleteSeRoleById(id);
        return SeCommonResponse.success();
    }

    @ApiOperation("新增角色人员权限")
    @PostMapping("insertRoleUser")
    public SeCommonResponse insertRoleUser(
            @ApiParam(required = true) @RequestParam(required = true) Long roleId,
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = true) @RequestParam(required = true) String userId,
            @ApiParam(required = true) @RequestParam(required = true) String userName
    ) {
        try {
            seRoleService.insertRoleUser(serviceCode,roleId,userId,userName);
        } catch (SeSecurityException e) {
            return SeCommonResponse.fromException(e);
        }
        return SeCommonResponse.success();
    }

    @ApiOperation("删除角色人员权限")
    @DeleteMapping("deleteRoleUser")
    public SeCommonResponse deleteRoleUser(
            @ApiParam(required = true) @RequestParam(required = true) Long id
    ) {
        seRoleService.deleteRoleUser(id);
        return SeCommonResponse.success();
    }

    @ApiOperation("角色列表")
    @GetMapping("selectRolePage")
    public SeCommonResponse<List<SeRole>> selectRolePage(
            @ApiParam(required = false) @RequestParam(required = false) String roleName,
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode
    ) {
        return SeCommonResponse.success(seRoleService.selectRoleList(serviceCode,roleName));
    }

    @ApiOperation("角色菜单列表")
    @GetMapping("selectRoleMenu")
    public SeCommonResponse<List<SeRoleMenu>> selectRoleMenu(
            @ApiParam(required = true) @RequestParam(required = true) Long id
    ) {
        return SeCommonResponse.success(seRoleService.selectRoleMenuList(id));
    }

    @ApiOperation("角色人员列表")
    @GetMapping("selectRoleUserList")
    public SeCommonResponse<List<SeRoleUser>> selectRoleUserList(
            @ApiParam(required = true) @RequestParam(required = true) Long roleId,
            @ApiParam(required = false) @RequestParam(required = false) String userName
    ) {
        return SeCommonResponse.success(seRoleService.selectRoleUserList(roleId,userName));
    }
}
